package com.wyz.saas.app

import android.text.TextUtils
import android.util.Log
import com.atom.api.ApiContext
import com.tencent.mmkv.MMKV

object Account {

    const val SAOP_IS_LOGIN = 2

    // 设备的推送Id
    @Volatile
    private var pushId: String? = null

    // 设备Id是否已经绑定到了服务器
    @Volatile
    private var isBind = false

    // 登录状态的Token，用来接口请求
    @Volatile
    private var token: String? = null

    // 登录的用户ID TODO 暂且这样
    @Volatile
    private var userId: String? = null

    // 登录的账户 TODO 暂且这样
    @Volatile
    private var account: String? = null

    // 账户描述 TODO 暂且这样
    @Volatile
    private var desc: String? = null

    // 账户头像 TODO 暂且这样
    @Volatile
    private var portrait: String? = null

    // 登录性别  TODO 暂且这样
    @Volatile
    private var sex: Int? = null

    private lateinit var context: ApiContext

    /**
     * 存储数据到XML文件，持久化
     */
    private fun save(context: ApiContext) {
        MMKV.defaultMMKV().also {
            // 存储数据
            it.edit()
                .putString(KEY_PUSH_ID, pushId)
                .putBoolean(KEY_IS_BIND, isBind)
                .putString(KEY_TOKEN, token)
                .putString(KEY_USER_ID, userId)
                .putString(KEY_ACCOUNT, account)
                .putString(KEY_DESC, desc)
                .putString(KEY_PORTRAIT, portrait)
                .putInt(KEY_SEX, sex ?: -1)
                .apply()
        }
    }


    fun load() {
        MMKV.defaultMMKV().apply {
            pushId = getString(KEY_PUSH_ID, "")
            isBind = getBoolean(KEY_IS_BIND, false)
            token = getString(KEY_TOKEN, "")
            userId = getString(KEY_USER_ID, "")
            account = getString(KEY_ACCOUNT, "")
            desc = getString(KEY_DESC, "")
            portrait = getString(KEY_PORTRAIT, "")
            sex = getInt(KEY_SEX, -1)
        }
    }

    /**
     * 设置并存储设备的Id
     * @param pushId 设备的推送ID
     */
    fun setPushId(pushId: String?) {
        this.pushId = pushId
        save(context)
    }

    fun getPushId(): String? {
        return pushId
    }

    /**
     * 返回当前账户是否登录 用户Id 和 Token 不为空
     * @return True已登录
     */
    fun isLogin(): Boolean {
        return (!TextUtils.isEmpty(userId) && !TextUtils.isEmpty(token))
    }

    /**
     * 是否已经完善了用户信息
     * @return True 是完成了
     */
    fun isComplete(): Boolean {
        // 首先保证登录成功
        if (isLogin()) {
            Log.e("Account", " isLogin() true  1 ")
            return (!TextUtils.isEmpty(desc)
                    && !TextUtils.isEmpty(portrait)
                    && sex != -1)
        }
        Log.e("Account", " isLogin() false  1 ")
        // 未登录返回信息不完全
        return false
    }

    /**
     * 是否已经绑定到了服务器
     * @return True已绑定
     */
    fun isBind(): Boolean {
        return isBind
    }

    /**
     * 设置绑定状态
     */
    fun setBind(isBind: Boolean) {
        this.isBind = isBind
        save(context)
    }

    /**
     * 保存我自己的信息到持久化XML中
     * 存储当前登录的账户, token, 用户Id，方便从数据库中查询我的信息
     */
//    fun login(model: AccountCard) {
//        token = model.getToken()
//        account = model.getAccount()
//        val user = model.getConsumer()
//        userId = user.getId()
//        desc = user.getDesc()
//        portrait = user.getPortrait()
//        sex = user.getSex()
//        save(context)
//    }

//    fun update(model: UserCard) {
//        desc = model.getDesc()
//        portrait = model.getPortrait()
//        sex = model.getSex()
//        save(context)
//    }

    fun logout(context: ApiContext) {
        token = null
        account = null
        userId = null
        isBind = false
        desc = null
        portrait = null
        sex = -1
        save(context)
    }


    fun getToken(): String? {
        return token
    }

    fun getUserId(): String? {
        return userId
    }

    fun getAccount(): String? {
        return account
    }

    fun getDesc(): String? {
        return desc
    }

    fun getPortrait(): String? {
        return portrait
    }

    fun getSex(): Int? {
        return sex
    }
}